package top.lishuoboy.jdbc.a2conn;

import cn.hutool.core.io.resource.ResourceUtil;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import lombok.extern.slf4j.Slf4j;

import javax.sql.DataSource;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties;

/**
 * JDBC工具类
 *
 * @author lishuoboy
 * @date 2021-12-19 18:00
 */
@Slf4j
public class JDBCUtil {

    private static Properties props = new Properties();

    static {
        try {
            props.load(ResourceUtil.getStream("db.properties"));
        } catch (IOException e) {
            log.error("异常", e);
        }
    }

    /** 数据源 */
    private static DataSource ds;
    /** 数据库连接池初始 DataSource */
    static {
        try {
            Properties props = new Properties();
            props.load(ResourceUtil.getStream("druid.properties"));
            ds = DruidDataSourceFactory.createDataSource(props);
        } catch (Exception e) {
            log.error("异常", e);
        }
    }



    /**
     * 获取数据库连接
     *
     * @author lishuoboy
     * @date 2021-12-19 18:45
     */
    public static Connection getConn() throws Exception {
        // 0.参数
        String driveClass5 = props.getProperty("driveClass5");  // mysql5
        String driveClass = props.getProperty("driveClass");  // mysql8
        String url = props.getProperty("url");
        String user = props.getProperty("user");
        String pwd = props.getProperty("pwd");


        // 1.加载驱动
        // 1.1 不是面相接口，写死了具体实现类，不灵活
//        Driver driver = new com.mysql.cj.jdbc.Driver();
        // 1.2 面相接口，反射、灵活
//        Driver driver = (Driver) Class.forName(driveClass).newInstance();
        Class.forName(driveClass);  // 对于部分数据库驱动，这行可以省略，因为下面的DriverManager.getConnection()会自动加载所有 Driver接口的实现类，遍历尝试创建连接，直到成功。jar包中/META-INF/service/接口名字


        // 2.获取连接
        // 2.1 方式一，driver.connect,提供Properties的对象，指明用户名和密码
//        Properties prop = new Properties();
//        prop.setProperty("user", user);
//        prop.setProperty("password", pwd);
//        Connection conn = driver.connect(url, prop);
        // 2.2.方式二
//        DriverManager.registerDriver(driver); // 注册驱动,此处可以省略，因为 com.mysql.cj.jdbc.Driver已经再静态块中调用了
        Connection conn = DriverManager.getConnection(url, user, pwd);
        return conn;
    }


    /**
     * 获取数据库连接
     *
     * @author lishuoboy
     * @date 2021-12-19 18:45
     */
    public static Connection getConn2() throws Exception {
        return ds.getConnection();
    }

    /**
     * 关闭资源,注意资源关闭顺序
     * 参考 hutool DbUtil.close(Object... objsToClose)
     *
     * @author lishuoboy
     * @date 2021-12-19 18:47
     */
    public static void close(AutoCloseable... objsToClose) {
        for (AutoCloseable autoCloseable : objsToClose) {
            try {
                if (autoCloseable != null) {
                    autoCloseable.close();
                }
            } catch (Exception e) {
                log.error("关闭资源异常", e);
            }
        }
    }

    public static DataSource getDs() {
        return ds;
    }
}
